Skip to content

[BOLT] Improve exception handling in NFC-Mode #146513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: users/paschalis-mpeis/nfc-check-remove-wrapper-logic
Choose a base branch
from

Conversation

paschalis-mpeis
Copy link
Member

@paschalis-mpeis paschalis-mpeis commented Jul 1, 2025

This patch introduce the following improvements:

  • Catch an exception when the CMakeCache.txt is not present
  • Bail out gracefully when llvm-bolt did not build successfully the
    current or previous revision.
  • Always do a --switch-back even if building the old revision failed

@paschalis-mpeis paschalis-mpeis changed the base branch from main to users/paschalis-mpeis/nfc-check-remove-wrapper-logic July 1, 2025 11:42
Copy link

github-actions bot commented Jul 1, 2025

✅ With the latest revision this PR passed the Python code formatter.

@paschalis-mpeis paschalis-mpeis changed the title Users/paschalis mpeis/nfc check improve file handling [BOLT] Improve file handling in NFC-Mode Jul 1, 2025
@paschalis-mpeis paschalis-mpeis marked this pull request as ready for review July 1, 2025 12:16
@llvmbot llvmbot added the BOLT label Jul 1, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-bolt

Author: Paschalis Mpeis (paschalis-mpeis)

Changes

This patch introduce the following improvements:

  • Catch an exception when the CMakeCache.txt is not present
  • Bail out gracefully when llvm-bolt did not build successfully the
    current or previous revision.

Full diff: https://github.com/llvm/llvm-project/pull/146513.diff

1 Files Affected:

  • (modified) bolt/utils/nfc-check-setup.py (+22-7)
diff --git a/bolt/utils/nfc-check-setup.py b/bolt/utils/nfc-check-setup.py
index 4884e616a0fa3..497f3997545ff 100755
--- a/bolt/utils/nfc-check-setup.py
+++ b/bolt/utils/nfc-check-setup.py
@@ -80,18 +80,27 @@ def main():
 
     source_dir = None
     # find the repo directory
-    with open(f"{args.build_dir}/CMakeCache.txt") as f:
-        for line in f:
-            m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
-            if m:
-                source_dir = m.groups()[0]
-    if not source_dir:
-        sys.exit("Source directory is not found")
+    try:
+        CMCacheFilename = f"{args.build_dir}/CMakeCache.txt"
+        with open(CMCacheFilename) as f:
+            for line in f:
+                m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
+                if m:
+                    source_dir = m.groups()[0]
+        if not source_dir:
+            raise Exception(f"Source directory not found: '{CMCacheFilename}'")
+    except Exception as e:
+        sys.exit(e)
 
     # build the current commit
+    print("NFC-Setup: Building current revision..")
     subprocess.run(
         shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
     )
+
+    if not os.path.exists(bolt_path):
+        sys.exit(f"Failed to build the current revision: '{bolt_path}'")
+
     # rename llvm-bolt
     os.replace(bolt_path, f"{bolt_path}.new")
     # memorize the old hash for logging
@@ -122,12 +131,18 @@ def main():
     subprocess.run(shlex.split(f"git checkout -f {args.cmp_rev}"), cwd=source_dir)
     # get the parent commit hash for logging
     new_ref = get_git_ref_or_rev(source_dir)
+
     # build the previous commit
+    print("NFC-Setup: Building previous revision..")
     subprocess.run(
         shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
     )
+
     # rename llvm-bolt
+    if not os.path.exists(bolt_path):
+        sys.exit(f"Failed to build the previous revision: '{bolt_path}'")
     os.replace(bolt_path, f"{bolt_path}.old")
+
     if args.switch_back:
         if stash:
             subprocess.run(shlex.split("git stash pop"), cwd=source_dir)

@paschalis-mpeis paschalis-mpeis marked this pull request as draft July 1, 2025 13:41
@paschalis-mpeis
Copy link
Member Author

Deals cases like this:

The previous revision had build failures which caused an exception during renaming.
This caused the NFC-Mode to stay in the old revision.

@paschalis-mpeis paschalis-mpeis marked this pull request as ready for review July 1, 2025 13:51
Copy link
Contributor

@aaupov aaupov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this.

This patch introduce the following improvements:
- Catch an exception when the CMakeCache.txt is not present
- Bail out gracefully when llvm-bolt did not build successfully the
  current or previous revision.
@paschalis-mpeis paschalis-mpeis force-pushed the users/paschalis-mpeis/nfc-check-improve-file-handling branch from 6648b77 to 09363a8 Compare July 3, 2025 09:55
@paschalis-mpeis paschalis-mpeis changed the title [BOLT] Improve file handling in NFC-Mode [BOLT] Improve exception handling in NFC-Mode Jul 3, 2025
@paschalis-mpeis
Copy link
Member Author

paschalis-mpeis commented Jul 3, 2025

Forced-push to rebase since the parent PR now has a --create-wrapper flag.

In the latest patch, switch_back is a function called whenever something goes wrong after checking out the prev revision, ie:

  • building the old binary fails, or
  • setting up the wrapper fails.

I also delete llvm-bolt at the start, since we rebuild it for the current revision anyway.


Update: @aaupov, requested review again so you are aware of the changes.

@paschalis-mpeis paschalis-mpeis requested a review from aaupov July 3, 2025 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants